package com.ruoyi.guest.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.guest.domain.Guest;
import com.ruoyi.guest.service.IGuestService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 顾客信息Controller
 * 
 * @author ruoyi
 * @date 2025-05-31
 */
@RestController
@RequestMapping("/ruoyi/guest")
public class GuestController extends BaseController
{
    @Autowired
    private IGuestService guestService;

    /**
     * 获取当前登录顾客的个人信息
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:guest:guest_profile')") // 权限控制
    @GetMapping("/guest_profile")
    public AjaxResult guestProfile() {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();

        // 通过用户ID查询关联的员工信息（需实现关联逻辑）
        Guest guest = guestService.selectGuestByUserId(loginUser.getUserId());

        if (guest == null) {
            return AjaxResult.error("顾客信息不存在");
        }
        return AjaxResult.success(guest);
    }

    /**
     * 查询顾客信息列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:guest:list')")
    @GetMapping("/list")
    public TableDataInfo list(Guest guest)
    {
        startPage();
        List<Guest> list = guestService.selectGuestList(guest);
        return getDataTable(list);
    }

    /**
     * 导出顾客信息列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:guest:export')")
    @Log(title = "顾客信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Guest guest)
    {
        List<Guest> list = guestService.selectGuestList(guest);
        ExcelUtil<Guest> util = new ExcelUtil<Guest>(Guest.class);
        util.exportExcel(response, list, "顾客信息数据");
    }

    /**
     * 获取顾客信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:guest:query')")
    @GetMapping(value = "/{guestId}")
    public AjaxResult getInfo(@PathVariable("guestId") Long guestId)
    {
        return success(guestService.selectGuestByGuestId(guestId));
    }

    /**
     * 新增顾客信息
     *
     * 增加注解 @Validated
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:guest:add')")
    @Log(title = "顾客信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Guest guest)
    {

        return toAjax(guestService.insertGuest(guest));
    }

    /**
     * 修改顾客信息
     * 增加注解 @Validated
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:guest:edit')")
    @Log(title = "顾客信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated@RequestBody Guest guest)
    {
        return toAjax(guestService.updateGuest(guest));
    }

    /**
     * 删除顾客信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:guest:remove')")
    @Log(title = "顾客信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guestIds}")
    public AjaxResult remove(@PathVariable Long[] guestIds)
    {
        return toAjax(guestService.deleteGuestByGuestIds(guestIds));
    }

    /**
     * 重置顾客密码
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:guest:edit')")
    @Log(title = "重置顾客密码信息", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd/{guest_id}")
    public AjaxResult resetPwd(@PathVariable Long guest_id)
    {
        Guest guest = new Guest();
        guest.setGuestId(guest_id);
        guest.setPassword(SecurityUtils.encryptPassword("123456")); // 密码加密 初始密码
        // 调用更新密码
        return toAjax(guestService.updateGuest(guest));
    }
}
